/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addGUI.java
 *
 * Created on 2010-06-13, 22:35:18
 */

package client;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author piotrek
 */
public class addGUI extends javax.swing.JFrame {

    /** Creates new form addGUI */
    public addGUI() {
        initComponents();
        jComboBox1.removeAllItems();
        jComboBox2.removeAllItems();
        jComboBox3.removeAllItems();
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jTextField6 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("jLabel1");

        jTextField1.setText("jTextField1");

        jLabel2.setText("jLabel2");

        jLabel3.setText("jLabel3");

        jTextField2.setText("jTextField2");

        jTextField3.setText("jTextField3");

        jButton1.setText("Dodaj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jCheckBox1.setText("jCheckBox1");

        jCheckBox2.setText("jCheckBox2");

        jLabel4.setText("jLabel4");

        jLabel5.setText("jLabel5");

        jTextField4.setText("jTextField4");

        jTextField5.setText("jTextField5");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jTextField6.setText("jTextField6");

        jLabel6.setText("jLabel6");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTextField6)
                    .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, 170, Short.MAX_VALUE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox3, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox1)
                            .addComponent(jCheckBox2))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox2)))
                .addGap(12, 12, 12))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (reference instanceof Zajezdnie && row==null) {
            try {
                if (((String)jComboBox1.getSelectedItem()).compareTo("Zajezdnia tramwajowa")==0)
                    ((Zajezdnie) reference).addT(jTextField1.getText(),jTextField2.getText(),Integer.valueOf(jTextField3.getText()));
                else
                    ((Zajezdnie) reference).addA(jTextField1.getText(),jTextField2.getText(),Integer.valueOf(jTextField3.getText()));
            }
            catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this,"Nieprawidłowe dane\n"+ex.getMessage() );
                return;
            }
            catch (SQLException ex) {
                JOptionPane.showMessageDialog(this,"Błąd połączenia z bazą danych\n"+ex.getMessage() );
                return;
            }
        }
        else if (reference instanceof Zajezdnie && row!=null) {
            try {
                int oldtype=-1;
                if (String.valueOf(row[4]).compareTo("Autobusowa")==0)
                    oldtype=Static.AUTOBUS;
                if (String.valueOf(row[4]).compareTo("Tramwajowa")==0)
                    oldtype=Static.TRAMWAJ;
                ((Zajezdnie) reference).modify(row[0], jTextField1.getText(),
                        jTextField2.getText(), Integer.valueOf(jTextField3.getText()),
                        jComboBox1.getSelectedIndex(),oldtype);
            }
            catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this,"Nieprawidłowe dane\n"+ex.getMessage() );
                return;
            }
            catch (SQLException ex) {
                JOptionPane.showMessageDialog(this,"Błąd połączenia z bazą danych\n"+ex.getMessage() );
                return;
            }
        }
        if (reference instanceof Pojazdy && row==null) {
            int klim,nisk;
            if (jCheckBox1.isSelected()) klim=1;
            else klim=0;
            if (jCheckBox2.isSelected()) nisk=1;
            else nisk=0;
            try {
                if (((String)jComboBox1.getSelectedItem()).compareTo("Zajezdnia tramwajowa")==0)
                    ((Pojazdy)reference).addT(jTextField1.getText(),jTextField2.getText(),Integer.valueOf(jTextField3.getText()));
                else
                    ((Pojazdy)reference).addA(jTextField1.getText(),jTextField2.getText(),
                            jTextField3.getText(),Integer.valueOf(jTextField4.getText()),
                            Integer.valueOf(jTextField5.getText()),klim, nisk,
                            tmp_slo.getElements().get(jComboBox3.getSelectedIndex())[0],
                            tmp_zaj.getElements().get(jComboBox2.getSelectedIndex())[0]);
            }
            catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this,"Nieprawidłowe dane\n"+ex.getMessage() );
                return;
            }
            catch (SQLException ex) {
                JOptionPane.showMessageDialog(this,"Błąd połączenia z bazą danych\n"+ex.getMessage() );
                return;
            }
            
        }
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        if (((String)jComboBox1.getSelectedItem()).compareTo("Zajezdnia tramwajowa")==0)
            jComboBox3.setEnabled(false);
        else
            
    }//GEN-LAST:event_jComboBox1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addGUI().setVisible(true);
            }
        });
    }
    
    
    public void setReference(Module m){
        System.out.println(m instanceof Pojazdy);
        reference=m;
        if (reference instanceof Zajezdnie) {
            jLabel1.setText("Nazwa");
            jLabel2.setText("Adres");
            jLabel3.setText("Pojemność");
            jLabel4.setVisible(false);
            jLabel5.setVisible(false);
            jLabel6.setVisible(false);
            jTextField4.setVisible(false);
            jTextField5.setVisible(false);
            jTextField6.setVisible(false);
            jCheckBox1.setVisible(false);
            jCheckBox2.setVisible(false);
            jComboBox2.setVisible(false);
            jComboBox3.setVisible(false);


            jComboBox1.addItem("Zajezdnia tramwajowa");
            jComboBox1.addItem("Zajezdnia autobusowa");
            if (row==null) {
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
            }
            else {
                jTextField1.setText(String.valueOf(row[1]));
                jTextField2.setText(String.valueOf(row[2]));
                jTextField3.setText(String.valueOf(row[3]));
                if (String.valueOf(row[4]).compareTo("Autobusowa")==0) {
                    jComboBox1.setSelectedIndex(1);
                }
                jButton1.setText("Modyfikuj");
            }
        }
        if (reference instanceof Pojazdy) {
            for (int i=0;i<jPanel1.getComponentCount();i++)
                jPanel1.getComponents()[i].setVisible(true);
            jLabel1.setText("Model");
            jLabel2.setText("Nr rejestracyjny");
            jLabel3.setText("Data zakupu");
            jLabel4.setText("Pojemność");
            jLabel5.setText("Miejsc");
            jLabel6.setText("Liczba wagonów");
            jCheckBox1.setText("Klimatyzacja");
            jCheckBox2.setText("Niskopodłogowy");
            jComboBox1.addItem("Zajezdnia tramwajowa");
            jComboBox1.addItem("Zajezdnia autobusowa");
            try {
                tmp_zaj = new Zajezdnie(reference.getUsername(), reference.getPassword(), reference.getServer());
                tmp_zaj.loadTable();
                ArrayList<Object[]> tmp_set=tmp_zaj.getElements();
                for (int i=0;i<tmp_set.size();i++)
                    jComboBox2.addItem(tmp_set.get(i)[1]);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this,"Nie można pobrać zajezdni z bazy danych.\nBłąd: "+ex.getMessage() );
                return;
            }
            try {
                tmp_slo = new Slowniki(reference.getUsername(), reference.getPassword(), reference.getServer());
                tmp_slo.loadTable();
                ArrayList<Object[]> tmp_set=tmp_slo.getElements();
                for (int i=0;i<tmp_set.size();i++)
                    jComboBox3.addItem(tmp_set.get(i)[1]);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this,"Nie można pobrać typów autobusów z bazy danych.\nBłąd: "+ex.getMessage() );
                return;
            }
            jComboBox3.setEnabled(false);

            if (row==null) {
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                jTextField6.setText("");
            }
        }
    }

    private Module reference;

    private Zajezdnie tmp_zaj;

    private Slowniki tmp_slo;

    private Object[] row=null;

    public void setRow(Object[] row) {this.row=row;}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables

}
